


#include <material_editor/createnewshaderdialog.h>
#include <material_editor/changeshadertypedialog.h>
#include <material_editor/materialview.h>
#include <material_editor/shadereditor.h>
#include <genericdeletedialog.h>
#include <genericrenamedialog.h>
#include <genericsavedialog.h>
#include <session.h>
#include <stdio.h>


void MaterialView::ShaderEdit (Shader *shader, int line)
{
  ShaderEditor* editor = static_cast<ShaderEditor*>(OpenEditor(shader));
  if (!editor)
    {
      editor = new ShaderEditor(shader);
      InsertEditor(editor);
    }

  editor->SetLine (line);
}



void MaterialView::ShaderEditCurrent ()
{
  ShaderItem* item = get_current_item<ShaderItem>(_overview);
  if (!item)
    {
      return;
    }
  ShaderEdit (item->GetShader());
}

void MaterialView::ShaderChangeTypeCurrent()
{
  ShaderItem* item = get_current_item<ShaderItem>(_overview);
  if (!item)
    {
      return;
    }

  Shader* shader = item->GetShader();
  if (!shader)
    {
      return;
    }

  ChangeShaderTypeDialog dlg (this);
  dlg.SetShaderType(shader->GetShaderType());
  if (dlg.exec() == QDialog::Rejected)
    {
      return;
    }

  shader->SetShaderType(dlg.GetShaderType());
  shader->Save();

}



void MaterialView::ShaderAdd()
{
  CategoryItem* item = get_current_item<CategoryItem>(_overview);
  if (!item)
    {
      return;
    }

  CreateNewShaderDialog dlg (Session::Get()->GetMainWindow());
  dlg.SetName("Shader");
  dlg.SetShaderType (ST_Vertex);
  dlg.SetCategory(item->GetCategory());
  if (dlg.exec() == QDialog::Rejected)
    {
      return;
    }



  _selectNewItems = true;
  Shader* shader = _manager->CreateShader();
  shader->SetName(dlg.GetName());
  shader->SetCategory(dlg.GetCategory());
  shader->SetShaderType(dlg.GetShaderType());

  if (_manager->AddShader(shader))
    {
      ShaderEdit(shader);
    }
  _selectNewItems = false;
}


void MaterialView::ShaderAdded(Shader *shader)
{
  AddItem (new ShaderItem(this, shader), shader);
}

void MaterialView::EditShader(Shader *shader, int line)
{
  ShaderEdit(shader, line);
}
